<?php
namespace app\admin\controller;
use app\admin\model\Link as LinkModel;
use app\admin\validate\Link as LinkValidate;
/*
 * -- ----------------------------
 * --  链接管理
 * -- ----------------------------
 */
class Link extends Common
{
  /** 
   * 链接添加
   */
  public function add(){
    if(request()->isAjax()){

      $error = (new LinkValidate())->goCheck();
      if($error !== true){
        $back = [
          'statu' => 400,
          'info'  => $error
        ];
        return $back;
      }

      $data = input('post.');
      //数据添加
      $add = db('link')->insert($data);
      if($add){
        $back = [
          'statu' => 200,
          'info' => '添加成功'
        ];
      }else{
        $back = [
          'statu' => 400,
          'info' => '添加失败'
        ];
      }
      return $back;
    }
    return view();
  }

  /** 
   * 链接列表
   */
  public function lst(){
    $code = new LinkModel;
    $link = $code->paginate(10);
    $this->assign('link',$link);
    return view();
  }

  /** 
   * 链接修改
   */
  public function edit(){
    $code = new LinkModel;
    if(request()->isPost()){

      $error = (new LinkValidate())->goCheck();
      if($error !== true){
        $back = [
          'statu' => 400,
          'info'  => $error
        ];
        return $back;
      }

      $data = input('post.');
      $res = $code->where('id',input('id'))->update($data);
      if($res){
        $back = [
          'statu' => 200,
          'info' => '修改成功'
        ];
      }else{
        $back = [
          'statu' => 400,
          'info' => '修改失败'
        ];
      }
      return $back;
    }
    $link = $code->find(input('id'));
    $this->assign('link',$link);
    return view();
  }

  /** 
   * 链接ajax删除
   */
  public function del($id){
    $id = input("post.id/d");
    $res = db('link')->where('id',$id)->delete();
    if($res){
      $data = [
        'statu' => 200,
        'info'  => '删除成功'
      ];
    }else{
      $data = [
        'statu' => 400,
        'info'  => '删除失败'
      ];
    }
    return $data;
  }

}
?>